/*
 * Created by JFormDesigner on Tue Sep 07 11:08:43 CST 2021
 */

package com.minelx.woods.pc.form;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.minelx.sqlstructures.common.Files;
import com.minelx.woods.pc.form.values.VariantValues;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Map;
import java.util.Objects;

import static java.util.stream.Collectors.toMap;

/**
 * @author aaa
 */
public class AddingVariantCollectionsDialog extends JDialog {

	private final JSONObject variantMappings;

	private final DefaultListModel<String> availableFieldsModel;

	private final DefaultListModel<VariantValues> variantCollectionsModel;

	public AddingVariantCollectionsDialog(Window owner, DefaultListModel<VariantValues> variantCollectionsModel) {
		super(owner);
		this.variantCollectionsModel = variantCollectionsModel;
		initComponents();

		variantMappings = JSON.parseObject(Files.toString("/mappings/child_sql_variables_mappings.json"));

		// preparing model
		availableFieldsModel = createAvailableFieldsModel();
		availableFieldsList.setModel(availableFieldsModel);
		variantCollectionsList.setModel(variantCollectionsModel);

		// set variant collections list cell renderer
		DefaultListCellRenderer renderer = new DefaultListCellRenderer();
		variantCollectionsList.setCellRenderer((list, value, index, isSelected, cellHasFocus) -> renderer.getListCellRendererComponent(list,
				value.getName() + ", 参数: " + value.getVariables(),
				index,
				isSelected,
				cellHasFocus));
	}

	private void addButtonActionPerformed(ActionEvent e) {
		// added field name
		String fieldName = availableFieldsModel.getElementAt(availableFieldsList.getSelectedIndex());
		Map<String, String> variables = variantMappings.getJSONArray(fieldName).toJavaList(JSONObject.class)
				.stream().map(variableAsJSON -> variableAsJSON.getString("name"))
				.collect(toMap(
						variableName -> variableName,
						variableName -> candyOrJoke("参数 " + variableName + " 的值为: ", this)
				));
		variantCollectionsModel.addElement(new VariantValues(fieldName, variables));
	}

	private void deleteButtonActionPerformed(ActionEvent e) {
		variantCollectionsModel.removeElementAt(variantCollectionsList.getSelectedIndex());
	}

	private DefaultListModel<String> createAvailableFieldsModel() {
		DefaultListModel<String> result = new DefaultListModel<>();
		result.addAll(variantMappings.keySet());
		return result;
	}

	private static String candyOrJoke(String prompt, AddingVariantCollectionsDialog owner) {
		return Objects.requireNonNull(JOptionPane.showInputDialog(owner, prompt));
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		panel1 = new JPanel();
		scrollPane2 = new JScrollPane();
		availableFieldsList = new JList<>();
		label1 = new JLabel();
		panel2 = new JPanel();
		scrollPane3 = new JScrollPane();
		variantCollectionsList = new JList<>();
		label2 = new JLabel();
		buttonBar = new JPanel();
		addButton = new JButton();
		deleteButton = new JButton();

		//======== this ========
		setTitle("\u6dfb\u52a0\u53c2\u6570\u5b57\u6bb5");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		var contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), 5, -1));

				//======== panel1 ========
				{
					panel1.setLayout(new BorderLayout());

					//======== scrollPane2 ========
					{

						//---- availableFieldsList ----
						availableFieldsList.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
						scrollPane2.setViewportView(availableFieldsList);
					}
					panel1.add(scrollPane2, BorderLayout.CENTER);

					//---- label1 ----
					label1.setText("\u53ef\u7528\u5b57\u6bb5");
					label1.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 23));
					panel1.add(label1, BorderLayout.NORTH);
				}
				contentPanel.add(panel1, new GridConstraints(0, 0, 1, 1,
					GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
					GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
					GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
					null, null, null));

				//======== panel2 ========
				{
					panel2.setLayout(new BorderLayout());

					//======== scrollPane3 ========
					{

						//---- variantCollectionsList ----
						variantCollectionsList.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
						scrollPane3.setViewportView(variantCollectionsList);
					}
					panel2.add(scrollPane3, BorderLayout.CENTER);

					//---- label2 ----
					label2.setText("\u5df2\u6dfb\u52a0");
					label2.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 23));
					panel2.add(label2, BorderLayout.NORTH);
				}
				contentPanel.add(panel2, new GridConstraints(0, 1, 1, 1,
					GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
					GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
					GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
					null, null, null));
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			//======== buttonBar ========
			{
				buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
				buttonBar.setLayout(new GridBagLayout());
				((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 0};
				((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

				//---- addButton ----
				addButton.setText("\u6dfb\u52a0");
				addButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
				addButton.addActionListener(e -> addButtonActionPerformed(e));
				buttonBar.add(addButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(0, 0, 0, 5), 0, 0));

				//---- deleteButton ----
				deleteButton.setText("\u5220\u9664");
				deleteButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
				deleteButton.addActionListener(e -> deleteButtonActionPerformed(e));
				buttonBar.add(deleteButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(0, 0, 0, 0), 0, 0));
			}
			dialogPane.add(buttonBar, BorderLayout.SOUTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		setSize(670, 535);
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JPanel panel1;
	private JScrollPane scrollPane2;
	private JList<String> availableFieldsList;
	private JLabel label1;
	private JPanel panel2;
	private JScrollPane scrollPane3;
	private JList<VariantValues> variantCollectionsList;
	private JLabel label2;
	private JPanel buttonBar;
	private JButton addButton;
	private JButton deleteButton;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
